<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      //父类原型
      function Person(name) {
        this.name=name;//只能继承原型
      }
      Person.prototype.type = "中国人";
      Person.prototype.eat = function () {
        console.log(this.type + "喜欢吃米饭");
      };

      // 子类
      function Teacher() {}
      //子类继承父类原型
      Teacher.prototype = new Person();
      // Teacher.prototype = Person.prototype; 或
      Teacher.prototype.name = "老王";
      Teacher.prototype.teach = function (course) {
        console.log(this.name + "主讲" + course);
      };
      // 让子类拥有自己的属性和方法
      var t1 = new Teacher();
      Person.prototype.sex = "男";//父类改变子类也会随之改变
      console.log(t1);
      t1.teach("前端");
    </script>
  </body>
</html>
